package overlays;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.drawable.Drawable;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.OverlayItem;

/**
 * This class represents an Overlay consisting of items to be added to the MapView.
 */
public class MyItemizedOverlay extends ItemizedOverlay<OverlayItem> {

	private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
	Context mContext;

	/**
	 * Constructor for this itemized overlay.
	 * @param defaultMarker the default location of this overlay
	 * @param context The context in which this overlay is being added
	 */
	public MyItemizedOverlay(Drawable defaultMarker, Context context) {
		super(boundCenterBottom(defaultMarker));
		mContext = context;
	}

	/**
	 * Returns the overlay item that is stored at the specified index.
	 * @param i the index of the item being created.
	 */
	@Override
	protected OverlayItem createItem(int i) {
		return mOverlays.get(i);
	}

	/**
	 * Returns the size of this overlay as an integer.
	 */
	@Override
	public int size() {
		return mOverlays.size();
	}

	/**
	 * Public method to add and item to this itemized overlay.
	 * @param the overlay item to be added to this overlay.
	 */
	public void addOverlay(OverlayItem overlay) {
		mOverlays.add(overlay);
		populate();
	}

	/**
	 * This method determines how a specific overlay item will react to the user tapping the screen
	 * in its location.
	 */
	@Override
	protected boolean onTap(int index) {
		OverlayItem item = mOverlays.get(index);
		AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
		dialog.setTitle(item.getTitle());
		dialog.setMessage(item.getSnippet());
		dialog.show();
		return true;
	}

	/**
	 * This method forces overlays to be draw in a secific order.
	 */
	@Override
	protected int getIndexToDraw(int drawingOrder) {
		return drawingOrder; // forces order to follow overlay item sequence
	}
}
